import type { ErrorMessageMode } from "#/axios";
import { useMessage } from "@/hooks/useMessage";
// import router from '/@/router';
// import { PageEnum } from '/@/enums/pageEnum';
import { useUserStoreHook } from "@/store/modules/user";
import { ERROR_TIPS } from "@/utils/staticData";

const { createMessage, createErrorModal } = useMessage();
const error = createMessage.error!;

export function checkStatus(
  status: number,
  msg: string,
  errorMessageMode: ErrorMessageMode = "message"
): void {
  const userStore = useUserStoreHook();
  let errMessage = "";

  switch (status) {
    case 400:
      errMessage = `${msg}`;
      break;
    // 401: Not logged in
    // Jump to the login page if not logged in, and carry the path of the current page
    // Return to the current page after successful login. This step needs to be operated on the login page.
    case 401:
      errMessage = msg || ERROR_TIPS.errMsg401;
      userStore.logOut();
      break;
    case 403:
      errMessage = ERROR_TIPS.errMsg403;
      break;
    // 404请求不存在
    case 404:
      errMessage = ERROR_TIPS.errMsg404;
      break;
    case 405:
      errMessage = ERROR_TIPS.errMsg405;
      break;
    case 408:
      errMessage = ERROR_TIPS.errMsg408;
      break;
    case 500:
      errMessage = ERROR_TIPS.errMsg500;
      break;
    case 501:
      errMessage = ERROR_TIPS.errMsg501;
      break;
    case 502:
      errMessage = ERROR_TIPS.errMsg502;
      break;
    case 503:
      errMessage = ERROR_TIPS.errMsg503;
      break;
    case 504:
      errMessage = ERROR_TIPS.errMsg504;
      break;
    case 505:
      errMessage = ERROR_TIPS.errMsg505;
      break;
    default:
  }

  if (errMessage) {
    if (errorMessageMode === "modal") {
      createErrorModal({ title: ERROR_TIPS.errorTip, message: errMessage });
    } else if (errorMessageMode === "message") {
      error(errMessage);
    }
  }
}
